package ali;

import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author keboom
 * @date 2021/4/29 14:12
 */
public class Test {
    private static int accountNums = 20;
    public static ConcurrentHashMap accountMap = new ConcurrentHashMap<String, Account>(accountNums);
    public static ExecutorService threadPool = Executors.newFixedThreadPool(10);

    public void transfer() {
        for (int i = 0; i < accountNums; i++) {
            accountMap.put("account" + i, new Account(100));
        }

        for (int i = 0; i < 10; i++) {
            threadPool.execute(new TransferMoney());
        }
    }

    public void print() {
        for (int i = 0; i < accountNums; i++) {
            Account accont = (Account) accountMap.get("account" + i);
            System.out.println(accont.getBalance());
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.transfer();
        if (threadPool.isTerminated()){
            test.print();
        }
    }
}
